package com.sanley.piss.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sanley.piss.mapper.BasicInfoMapper;
import com.sanley.piss.mapper.DictionaryDataMapper;
import com.sanley.piss.model.ServiceImpl;
import com.sanley.piss.model.StringUtil;
import com.sanley.piss.model.exceptionUtil.exception.ServiceException;
import com.sanley.piss.model.exceptionUtil.json.ResultEnum;
import com.sanley.piss.model.po.BasicInfo;
import com.sanley.piss.model.vo.BasicInfoVO;
import com.sanley.piss.web.service.mapper.IBasicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Create By  on 2021/3/22.
 *
 * @author 张凯大天才
 */
@Service
@Primary
public class BasicInfoServiceImpl extends ServiceImpl<BasicInfoMapper, BasicInfo> implements IBasicInfoService {

    @Autowired
    private DictionaryDataMapper mapper;

    @Override
    public void insert(@Valid BasicInfoVO basicInfo, Integer userId) {
        //查询姓名是否重复
        if (StringUtil.isNotNull(baseMapper.selectJobNum(null,basicInfo.getName()))) {
            throw new ServiceException(ResultEnum.userExist);
        }
        //查询工号是否重复
        if (StringUtil.isNotNull(baseMapper.selectJobNum(basicInfo.getJobNum(),null))) {
            throw new ServiceException(ResultEnum.jobNumExist);
        }
        basicInfo.setUserId(userId);
        basicInfo.setBloodType(mapper.selectArchivesTypes(basicInfo.getBloodTypeName(), "血型"));
        basicInfo.setBirthdayType(mapper.selectArchivesTypes(basicInfo.getBirthdayTypeName(), "生日类型"));
        basicInfo.setWorkingConditions(mapper.selectArchivesTypes(basicInfo.getWorkeTypeName(), "在职情况"));
        basicInfo.setContractType(mapper.selectArchivesTypes(basicInfo.getContractTypeName(), "合同类型"));
        basicInfo.setPositionId(mapper.selectArchivesTypes(basicInfo.getPosition(), "岗位类型"));
        basicInfo.setJob(mapper.selectArchivesTypes(basicInfo.getJobTypeName(), "职位类型"));
        basicInfo.setGmtCreate(LocalDateTime.now());
        baseMapper.insert(basicInfo);
    }

    @Override
    public void updateById(@Valid BasicInfoVO basicInfo) {
        basicInfo.setBloodType(mapper.selectArchivesTypes(basicInfo.getBloodTypeName(), "血型"));
        basicInfo.setBirthdayType(mapper.selectArchivesTypes(basicInfo.getBirthdayTypeName(), "生日类型"));
        basicInfo.setWorkingConditions(mapper.selectArchivesTypes(basicInfo.getWorkeTypeName(), "在职情况"));
        basicInfo.setContractType(mapper.selectArchivesTypes(basicInfo.getContractTypeName(), "合同类型"));
        basicInfo.setPositionId(mapper.selectArchivesTypes(basicInfo.getPosition(), "岗位类型"));
        basicInfo.setJob(mapper.selectArchivesTypes(basicInfo.getJobTypeName(), "职位类型"));
        baseMapper.updateById(basicInfo);
    }

    @Override
    public List<BasicInfoVO> selectAll(Integer userId) {
        return baseMapper.selectAll(userId);
    }

    @Override
    public BasicInfoVO selectById(Integer id) {
        BasicInfoVO selectOne = baseMapper.selectById(id);
        if (StringUtil.isNull(selectOne)) {
            throw new ServiceException(ResultEnum.roleIsNull);
        }
        return selectOne;
    }

    @Override
    public void batchInsert(List<BasicInfo> list) {
        list.forEach(basicInfo->{
            //查询姓名是否重复
            if (StringUtil.isNotNull(baseMapper.selectJobNum(null,basicInfo.getName()))) {
                throw new ServiceException("用户已存在");
            }
            //查询工号是否重复
            if (StringUtil.isNotNull(baseMapper.selectJobNum(basicInfo.getJobNum(),null))) {
                throw new ServiceException(ResultEnum.jobNumExist);
            }
            basicInfo.setGmtCreate(LocalDateTime.now());
        });
        baseMapper.batchInsert(list);
    }

    @Override
    public PageInfo<BasicInfoVO> page(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BasicInfoVO> basicInfos = baseMapper.selectAllUser();
        PageInfo<BasicInfoVO> param = new PageInfo<>(basicInfos);
        return param;
    }

    @Override
    public void updateOrgByUserId(String orgName, Integer id) {
        Integer integer = baseMapper.updateOrgByUserId(orgName, id);
        if (integer != 1) {
            throw new ServiceException("部门不存在");
        }
    }
}
